package org.hbs.blog.code.controller;

import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hbs.blog.code.biz.IBlogBiz;
import org.hbs.blog.code.entity.Blog;
import org.hbs.blog.code.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller("blogController")
public class BlogController implements ControllerData {

	@Resource(name="blogBiz")
	private IBlogBiz blogBiz;
	
	private static final String _PORLET = "WEB-INF/view/portlet";
	
	@RequestMapping(value="/writeBlog")
	public ModelAndView writeBlog(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		User user = (User) request.getSession(true).getAttribute("currUser");
		
		Blog b = new Blog();
		b.setBlogID(UUID.randomUUID().toString());
		b.setUserID(user.getUserID());
		b.setBlogTypeID("a56b9b66-e2e5-4b95-806e-825196fbec03");
		b.setCreateDate(new Date());
		b.setBlogTitle(request.getParameter("title"));
		b.setBlogContent(request.getParameter("myContent"));
		
		
		if(null == b.getBlogID() || "".equals(b.getBlogID().trim()) ||
				null == b.getUserID() || "".equals(b.getUserID().trim()) ||
				null == b.getBlogTypeID() || "".equals(b.getBlogTypeID().trim()) ||
				null == b.getBlogTitle() || "".equals(b.getBlogTitle().trim()) ||
				null == b.getBlogContent() || "".equals(b.getBlogContent().trim()) ||
				b.getBlogContent().indexOf("</script>") != -1 || b.getBlogTitle().indexOf("</") != -1){
			return new ModelAndView("redirect:10000.jsp");
		}
		
		blogBiz.insertBlog(b);
		return new ModelAndView("redirect:index.jsp");
	}
	
	@RequestMapping(value="/queryBlogList")
	public ModelAndView queryBlogList(){
		return new ModelAndView("forward:"+_PORLET+"/blogList.jsp").addObject("blogs", blogBiz.selectAllBlog());
	}
	
	@RequestMapping(value="/queryBlog")
	public ModelAndView queryBlog(String blogID){
		return new ModelAndView("forward:"+_PORLET+"/blogDetail.jsp").addObject("blog", blogBiz.selectBlogByID(blogID));
	}
	
	@RequestMapping(value="deleteBlog")
	public ModelAndView deleteBlog(String blogID){
		//blogBiz.deleteBlogByID(blogID);
		return new ModelAndView("redirect:index.jsp");
	}
}
